USE [master]
GO

CREATE DATABASE Legacy;
GO

USE [Legacy]
GO

/****** Object:  Table [dbo].[UnidadesMedida]    Script Date: 29/09/2014 21:18:06 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[UnidadesMedida](
	[Codigo] [varchar](2) NOT NULL,
	[Nome] [varchar](15) NOT NULL,
 CONSTRAINT [PK_UnidadesMedida] PRIMARY KEY CLUSTERED 
(
	[Codigo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

/****** Object:  Table [dbo].[FormasPagamento]    Script Date: 29/09/2014 21:18:27 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[FormasPagamento](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Nome] [varchar](20) NOT NULL,
	[QuantidadeParcelas] [smallint] NULL,
 CONSTRAINT [PK_FormasPagamento] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

/****** Object:  Table [dbo].[Produtos]    Script Date: 29/09/2014 21:18:44 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Produtos](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Codigo] [varchar](15) NOT NULL,
	[Nome] [varchar](50) NOT NULL,
	[Descricao] [varchar](max) NULL,
	[QuantidadeEstoque] [decimal](10, 2) NOT NULL,
	[CodigoUN] [varchar](2) NOT NULL,
 CONSTRAINT [PK_Produtos] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Produtos] ADD  CONSTRAINT [DF_Produtos_QuantidadeEstoque]  DEFAULT ((0)) FOR [QuantidadeEstoque]
GO

ALTER TABLE [dbo].[Produtos]  WITH CHECK ADD  CONSTRAINT [FK_Produtos_UnidadesMedida] FOREIGN KEY([CodigoUN])
REFERENCES [dbo].[UnidadesMedida] ([Codigo])
GO

ALTER TABLE [dbo].[Produtos] CHECK CONSTRAINT [FK_Produtos_UnidadesMedida]
GO

/****** Object:  Table [dbo].[Pessoas]    Script Date: 29/09/2014 21:19:09 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Pessoas](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[FisicaJuridica] [varchar](1) NOT NULL,
	[TipoPessoa] [varchar](1) NOT NULL,
	[Nome] [varchar](50) NOT NULL,
	[RazaoSocial] [varchar](50) NULL,
	[CpfCnpj] [varchar](18) NOT NULL,
	[Telefone] [varchar](20) NULL,
	[Email] [varchar](50) NULL,
	[Inativo] [bit] NULL,
 CONSTRAINT [PK_Pessoas] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

/****** Object:  Table [dbo].[Compras]    Script Date: 29/09/2014 21:19:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Compras](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[IdPessoa] [int] NOT NULL,
	[IdFormaPagamento] [int] NOT NULL,
	[DataCompra] [datetime] NOT NULL,
	[DataEntrega] [datetime] NULL,
	[ValorMercadoria] [decimal](12,2) NULL,
	[ValorImpostos] [decimal](12,2) NULL,
	[ValorDesconto] [decimal](12,2) NULL,
	[ValorTotalCompra] [decimal](12,2) NULL,
 CONSTRAINT [PK_Compras] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Compras]  WITH CHECK ADD  CONSTRAINT [FK_Compras_FormasPagamento] FOREIGN KEY([IdFormaPagamento])
REFERENCES [dbo].[FormasPagamento] ([ID])
GO

ALTER TABLE [dbo].[Compras] CHECK CONSTRAINT [FK_Compras_FormasPagamento]
GO

ALTER TABLE [dbo].[Compras]  WITH CHECK ADD  CONSTRAINT [FK_Compras_Pessoas] FOREIGN KEY([IdPessoa])
REFERENCES [dbo].[Pessoas] ([ID])
GO

ALTER TABLE [dbo].[Compras] CHECK CONSTRAINT [FK_Compras_Pessoas]
GO

/****** Object:  Table [dbo].[ItensCompra]    Script Date: 29/09/2014 21:20:04 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[ItensCompra](
	[IdCompra] [int] NOT NULL,
	[IdProduto] [int] NOT NULL,
	[Sequencia] [int] NOT NULL,
	[Quantidade] [decimal](10, 2) NOT NULL,
	[ValorICMS] [decimal](6, 2) NULL,
	[ValorIPI] [numeric](6, 2) NULL,
 CONSTRAINT [PK_ItensCompra] PRIMARY KEY CLUSTERED 
(
	[IdCompra] ASC,
	[IdProduto] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[ItensCompra]  WITH CHECK ADD  CONSTRAINT [FK_ItensCompra_Compras] FOREIGN KEY([IdCompra])
REFERENCES [dbo].[Compras] ([ID])
GO

ALTER TABLE [dbo].[ItensCompra] CHECK CONSTRAINT [FK_ItensCompra_Compras]
GO

ALTER TABLE [dbo].[ItensCompra]  WITH CHECK ADD  CONSTRAINT [FK_ItensCompra_Produtos] FOREIGN KEY([IdProduto])
REFERENCES [dbo].[Produtos] ([ID])
GO

ALTER TABLE [dbo].[ItensCompra] CHECK CONSTRAINT [FK_ItensCompra_Produtos]
GO

